import config from './config'
import store from '@/store'
import { getToken, setToken, removeToken, setSysUI } from '@/utils/auth'
import { getMctToken } from '@/api/system/user'
export function ssoLogin(to, from, next, path) {
  if (config.isSsoLogin) {
    let { clientId, ssoUrl, tokenUrl } = config
    window.__USERCENTER__.configUserCenter({
      clientId,
      ssoUrl,
      tokenUrl,
      // redirectUri
    })
    window.__USERCENTER__.login().then(async (res) => {
      await getMctToken({ accessToken: res.token }).then((res) => {
        store.commit('user/SET_TOKEN', res.data.token)
        store.commit('user/SET_LOGIN_MSG', null)
        setToken(res.data.token)
        next(to.fullPath)
      })

      // let passwordModified = true
      // if (data.hasOwnProperty('passwordModified')) {
      //   passwordModified = data.passwordModified
      // }
      // commit('SET_PASSWORD_MODIFIED', passwordModified)
      // localStorage.setItem('passwordModified', passwordModified)
    })
  } else {
    if (next) {
      next(path)
    }
  }
}

export function ssoLogout(cb) {
  debugger
  if (config.isSsoLogin) {
    let { clientId, ssoUrl, tokenUrl } = config
    window.__USERCENTER__.configUserCenter({
      clientId,
      ssoUrl,
      tokenUrl,
      // redirectUri
    })
    window.__USERCENTER__
      .logout()
      .then((res) => {})
      .catch((err) => {
        console.log(err, 'err')
      })
  } else if (cb) {
    cb()
  }
}
